#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Python 3.x
Flask 2.1.3
Flask-Restful 0.3.9

Official Manual Address : https://flask-restful.readthedocs.io/en/latest/

"""
import werkzeug
from flask import Flask
from flask_restful import Resource, Api
from flask_restful import reqparse
from flask_restful import marshal_with, fields

app = Flask(__name__)
api = Api(app=app)


class HelloWorld(Resource):
    def get(self):
        return {'Hello', 'World'}


# Hello World入门
api.add_resource(HelloWorld, '/')


class User(Resource):
    """
    用户相关请求
    """

    def get(self):
        """
        请求类型为Get
        :return:
        """
        pass

    def post(self):
        """
        请求类型为Post
        :return:
        """
        pass

    def put(self):
        """
        请求类型为Put
        :return:
        """
        pass

    def delete(self):
        """
        请求类型为Delete
        :return:
        """
        pass


# 资源丰富的路由
api.add_resource(User, '/user')

# 参数解析
from flask_restful import reqparse

parser = reqparse.RequestParser()
parser.add_argument('rate', type=int, help='Rate to charge for this resource')
# Look only in the POST body
parser.add_argument('name', type=int, location='form')
# Look only in the querystring
parser.add_argument('PageSize', type=int, location='args')
# From the request headers
parser.add_argument('User-Agent', location='headers')
# From http cookies
parser.add_argument('session_id', location='cookies')
# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')

args = parser.parse_args()


# 输出字段
class ProfileView(Resource):
    resource_fields = {
        'username': fields.String,
        'age': fields.Integer,
        'education': fields.String(attribute='school')
    }

    @marshal_with(resource_fields)
    def get(self, user_id):
        # 数据库User模型
        user = User.query.get(user_id)
        return user


if __name__ == '__main__':
    app.run()
